import os
import unittest
from netcool import ObjectServerDB

# Passed in from the command line - not to be used directly
_test_objserv_host = None
_test_objserv_port = 4100

def os_host():
    global _test_objserv_port
    return _test_objserv_host

def os_port():
    global _test_objserv_port
    return _test_objserv_port
    

class TestCase(unittest.TestCase):
    def os_host(self):
        return os_host()

    def os_port(self):
        return os_port()

    def execute(self, sql):
        conn = ObjectServerDB.connect(self.os_host(), self.os_port())
        curs = conn.cursor()
        curs.execute(sql)


def load_tests(tests_dir, name):
    tests = [t[0:-3] for t in os.listdir(tests_dir)
                        if t.startswith('test_') and t.endswith('.py')]

    all_tests = unittest.TestSuite()
    loader = unittest.TestLoader()
    for t in tests:
        modname = name + "." + t
        loaded = loader.loadTestsFromName(modname)
        all_tests.addTest(loaded)
    return all_tests


def run_tests(tests):
    return unittest.TextTestRunner(verbosity=2).run(tests)



tests_dir = os.path.join(os.path.dirname(__file__), '..', 'tests')
print "tests_dir", tests_dir
all_tests = load_tests(tests_dir, __name__)
